package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.EssayQuestion;
import com.ruoyi.recycle.domain.MerchantManagement;
import com.ruoyi.recycle.domain.PriceSetting;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.mapper.SysTypeConfigMapper;
import com.ruoyi.recycle.service.IEssayQuestionService;
import com.ruoyi.recycle.service.IMerchantManagementService;
import com.ruoyi.recycle.service.ISysTypeConfigService;
import com.ruoyi.recycle.service.PriceSettingService;
import com.ruoyi.recycle.vo.PriceSettingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统分类Service业务层处理
 *
 * @author Wubk
 * @date 2022-05-22
 */
@Service
public class SysTypeConfigServiceImpl extends ServiceImpl<SysTypeConfigMapper, SysTypeConfig> implements ISysTypeConfigService {
    @Autowired
    private SysTypeConfigMapper sysTypeConfigMapper;
    @Resource
    private IMerchantManagementService merchantManagementService;
    @Resource
    private IEssayQuestionService essayQuestionService;
    @Resource
    private PriceSettingService priceSettingService;

    /**
     * 查询系统分类
     *
     * @param id 系统分类主键
     * @return 系统分类
     */
    @Override
    public SysTypeConfig selectSysTypeConfigById(Long id) {
        return sysTypeConfigMapper.selectSysTypeConfigById(id);
    }

    /**
     * 查询系统分类列表
     *
     * @param sysTypeConfig 系统分类
     * @return 系统分类
     */
    @Override
    public List<SysTypeConfig> selectSysTypeConfigList(SysTypeConfig sysTypeConfig) {
        return sysTypeConfigMapper.selectSysTypeConfigList(sysTypeConfig);
    }

    /**
     * 新增系统分类
     *
     * @param sysTypeConfig 系统分类
     * @return 结果
     */
    @Override
    public int insertSysTypeConfig(SysTypeConfig sysTypeConfig) {
        return sysTypeConfigMapper.insertSysTypeConfig(sysTypeConfig);
    }

    /**
     * 修改系统分类
     *
     * @param sysTypeConfig 系统分类
     * @return 结果
     */
    @Override
    public int updateSysTypeConfig(SysTypeConfig sysTypeConfig) {
        return sysTypeConfigMapper.updateSysTypeConfig(sysTypeConfig);
    }

    /**
     * 批量删除系统分类
     *
     * @param ids 需要删除的系统分类主键
     * @return 结果
     */
    @Override
    public int deleteSysTypeConfigByIds(Long[] ids) {
        return sysTypeConfigMapper.deleteSysTypeConfigByIds(ids);
    }

    /**
     * 删除系统分类信息
     *
     * @param id 系统分类主键
     * @return 结果
     */
    @Override
    public int deleteSysTypeConfigById(Long id) {
        return sysTypeConfigMapper.deleteSysTypeConfigById(id);
    }


    @Override
    public AjaxResult oneKeyRecycle(Long id) {
        //一件回收页面内容详情
        //先判断是否存在综合站，不存在则只能回收且为快递，即没有家政服务
        //获取问答内容（无论是否综合站单子，都需要有）
        //获取回收内容（综合站存在则需要从综合站中获取）
        //获取家政服务（仅当为综合站时需要有）
        MerchantManagement merchantManagement = merchantManagementService.selectMerchantManagementById(id);
        EssayQuestion essayQuestion = new EssayQuestion();
        essayQuestion.setType("常见问题");
        List<EssayQuestion> essayQuestionList = essayQuestionService.selectEssayQuestionList(essayQuestion);

        if (ObjectUtils.isEmpty(merchantManagement) || 0 == id){
            //直邮订单
            List<SysTypeConfig> sysTypeConfigList = sysTypeConfigMapper.selectList(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType, "回收分类"));

            return AjaxResult.success().put("essayQuestionList",essayQuestionList).put("recycleDetail",sysTypeConfigList).put("housekeepingDetail",null);
        }else {
            //综合站订单

            List<PriceSetting> recycleDetail = priceSettingService.list(new LambdaQueryWrapper<PriceSetting>().eq(PriceSetting::getMerchantId, id).eq(PriceSetting::getCategory, "回收分类"));
            List<PriceSettingVo> housekeepingDetailVo = new ArrayList<>();
            List<SysTypeConfig> resultSysTypeConfigList = new ArrayList<>();
            if (recycleDetail.size()>0) {
                for (PriceSetting priceSetting : recycleDetail) {
                    SysTypeConfig sysTypeConfig = new SysTypeConfig();
                    List<SysTypeConfig> sysTypeConfigList = sysTypeConfigMapper.selectList(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType, "回收分类").eq(SysTypeConfig::getName, priceSetting.getName()));
                    if (sysTypeConfigList.size()>0){
                        BeanUtils.copyProperties(sysTypeConfigList.get(0),sysTypeConfig);
                        sysTypeConfig.setMoney(priceSetting.getPrice());
                    }else {
                        continue;
                    }
                    resultSysTypeConfigList.add(sysTypeConfig);
                }
            }

            List<PriceSetting> housekeepingDetail = priceSettingService.list(new LambdaQueryWrapper<PriceSetting>().eq(PriceSetting::getMerchantId, id).eq(PriceSetting::getCategory, "家政分类"));
            if (housekeepingDetail.size()>0) {
                for (PriceSetting priceSetting : housekeepingDetail) {
                    PriceSettingVo priceSettingVo = new PriceSettingVo();
                    BeanUtils.copyProperties(priceSetting,priceSettingVo);
                    List<SysTypeConfig> sysTypeConfigList = sysTypeConfigMapper.selectList(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType, "家政分类").eq(SysTypeConfig::getName, priceSetting.getHouseKeepingCategory()));
                    if (sysTypeConfigList.size()>0){
                        priceSettingVo.setImg(sysTypeConfigList.get(0).getImg());
                    }
                    housekeepingDetailVo.add(priceSettingVo);
                }
            }


            return AjaxResult.success().put("essayQuestionList",essayQuestionList).put("recycleDetail",resultSysTypeConfigList).put("housekeepingDetail",housekeepingDetailVo);

        }

    }
}
